package com.qgz.tools.utils;

import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupUtils {
    /**
     * 方法用途和描述: 模拟浏览器以Document类型返回被访问的网站源码
     *
     * @param url 被访问的website. 所传的URL必须以 "http://www."开头
     * @return doc 以Document类型返回被访问网页的html
     */
    public static Document getDocument(String url) {
        Document doc = null;
        int maxRetry = 10; // 最大重试次数
        for (int j = 1; j <= maxRetry; j++) {
            try {
                doc = Jsoup.connect(url)
                        .timeout(10 * 1000)
                        .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30")
                        .get();
                break;
            } catch (Exception ex) {
                Log.e("eee","正在尝试第"+j+"次，请求错误URL："+url);
            }
        }
        return doc;
    }

}
